//
//  DMConversationCell.m
//  BaseAPP
//
//  Created by Evisu on 16/12/12.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "DMConversationCell.h"

@interface DMConversationCell ()

//头像
@property (nonatomic, strong)UIImageView *ivHeadImage;

//昵称
@property (nonatomic, strong)UILabel *labNickName;

//消息
@property (nonatomic, strong)UILabel *labMessage;

//时间
@property (nonatomic, strong)UILabel *labTime;

//分割线
@property (nonatomic, strong)UIView *vline;

@end

@implementation DMConversationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self initViews];
        [self layoutViews];
    }
    
    return self;
}

- (void)initViews {

    self.ivHeadImage = [[UIImageView alloc] init];
    self.ivHeadImage.backgroundColor = RGBRandomColor;
    
    self.labNickName = [[UILabel alloc] init];
    self.labNickName.textColor = [UIColor blackColor];
    self.labNickName.font = [UIFont systemFontOfSize:18.0];
    
    self.labMessage = [[UILabel alloc] init];
    self.labMessage.textColor = HEXColor(@"A5A5A5");
    self.labMessage.font = [UIFont systemFontOfSize:16.0];
    self.labMessage.text = @"messagemessagemessagemessagemessagemessagemessagemessagemessagemessagemessagemessage";
    
    self.labTime = [[UILabel alloc] init];
    self.labTime.textColor = HEXColor(@"B0B0B0");
    self.labTime.font = [UIFont systemFontOfSize:12.0];
    self.labTime.text = @"凌晨3:23";
    
    self.vline = [[UIView alloc] init];
    self.vline.backgroundColor = KTABLEVIEW_SEPARATION_LINE;
    
    [self.contentView addSubview:self.ivHeadImage];
    [self.contentView addSubview:self.labNickName];
    [self.contentView addSubview:self.labMessage];
    [self.contentView addSubview:self.labTime];
    [self.contentView addSubview:self.vline];
    
}


- (void)layoutViews {

    self.ivHeadImage.sd_layout
    .centerYEqualToView(self.contentView)
    .leftSpaceToView(self.contentView, 10)
    .widthIs(46)
    .heightIs(46);
    
    self.labNickName.sd_layout
    .topSpaceToView(self.contentView, 14)
    .leftSpaceToView(self.ivHeadImage, 10)
    .heightIs(18);
    [self.labNickName setSingleLineAutoResizeWithMaxWidth:120];
    
    self.labMessage.sd_layout
    .bottomSpaceToView(self.contentView, 14)
    .leftSpaceToView(self.ivHeadImage, 10)
    .heightIs(16);
    [self.labMessage setSingleLineAutoResizeWithMaxWidth:self.contentView.width_sd-66-20];
    
    self.labTime.sd_layout
    .topSpaceToView(self.contentView, 12)
    .rightSpaceToView(self.contentView, 10);
    [self.labTime setSingleLineAutoResizeWithMaxWidth:100];
    
    self.vline.sd_layout
    .bottomEqualToView(self.contentView)
    .leftEqualToView(self.ivHeadImage)
    .rightEqualToView(self.contentView)
    .heightIs(1);
}


- (void)setConversation:(EMConversation *)conversation {

    _conversation = conversation;
    
    EMMessage *message = conversation.latestMessage;
    EMMessageBody *body = message.body;
    
    self.labNickName.text = message.conversationId;
    
    if (body.type == EMMessageBodyTypeText) {
        //文字消息
        EMTextMessageBody *textMessageBody = (EMTextMessageBody *)body;
        self.labMessage.text = textMessageBody.text;
        
        
    } else if (body.type == EMMessageBodyTypeImage) {
        //图片
        self.labMessage.text = @"[图片]";
    
    } else if (body.type == EMMessageBodyTypeVoice) {
        //语音
        self.labMessage.text = @"[语音]";
    
    }
}


@end
